# tornado全称：tornado web server,其拥有异步非阻塞IO的处理方式

# 导包
import time
# import numpy  #  计算
import tornado.web

import tornado.ioloop

# 服务器
from tornado import httpserver

import peewee_async  # 异步池  皮V AC可

# 导入基础类
from base import BaseHandler, WsBaseHandler
from db import User, database
# from mytornado.user_view import GiteeBack

# 导入用户试图
from user_view import *

from order_view import *

# 邮件方法
from tools import send_mail

# 导入文件操作类
import os

# 导入celery
from my_task import async_consume

# 设置tornado静态文件夹目录
static_path = os.path.join(os.path.dirname(__file__), 'static')


import json
import tornado.ioloop
import tornado.web
import tornado.httpserver

# 类视图
class TestHandler(BaseHandler):
    # class TestHandler(tornado.web.RequestHandler):

    # 声明异步
    async def get(self):
        # task = async_consume.delay()

        # 打印任务id
        # print(task.id)



        # await send_mail('2932746380@qq.com', '123', '123', 1)

        # num = self.get_argument('num',10)
        # print(1)

        # 异步获取多个
        # users = await self.application.objects.execute(User.select())

        # for user in users:
        #     print(user.username)

        # 获取余额  异步写法
        # time.sleep(1)
        # user = await self.application.objects.get(User,id=1)

        # user = User.get(id=1)

        # 添加数据 异步写法
        # user = await self.application.object.create(User,username='异步数据',age=20)

        # #修改数据
        # user.name = 500
        # await self.application.objects.update(user)

        # #删除
        # await self.application.objects.delete(user)

        # print(user)
        # 修改余额
        # user.num =int(num)+user.num
        # #保存
        # user.save()
        # user = await self.application.objects.update(user)
        # print(user)        

        # 接收参数
        # id = self.get_argument('id',None)
        # print(id)

        return self.write({'code': 200, 'msg': '获取成功'})


#     async def post(self):
#         
#         #接收参数
#         id = self.get_argument('id',None)
#         print(id)
#         
#         return self.write({'code':200,'msg':'添加成功'})
#     
#     async def delete(self):
#         #接收参数
#         id = self.get_argument('id',None)
#         print(id)
#         return self.write({'code':200,'msg':'删除成功'})
#     
#     async def put(self):
#         #接收参数
#         id = self.get_argument('id',None)
#         print(id)
#         return self.write({"code":2005,"msg":'修改成功'})

# 执行tornado


# 执行方法
def main():
    # 实例化tornado对象    handlers:试图   tuple:吐口   debug:调试,即改代码重新编译
    # 参数app用于告诉http服务器的路由
    app = tornado.web.Application(
        handlers=[
            # 路由  首页
            (r'/', TestHandler),
            (r'/ws/', WsHandler),

            # 用户模块

            (r'/userinfo/', UserInfo),
            (r'/role/', UserRole),
            # 文件上传
            (r'/upload/', Upload),
            # 广告物料
            (r'/adupload/', AdUpload),
            (r'/auditupdate/', AuditUpdate),
            (r'/useraudit/', UserAudit),
            (r'/putaudit/', PutAudit),
            # 主动推送数据
            (r'/push/', Push),
            # 获取聊天记录
            (r'/chatlist/', ChatList),
            # 客服路由
            (r'/client/', ClientHandler),
            # 出队
            (r'/outserver/', OutServer),
            # 客服主动出发出队接口
            (r'/outclient/', OutClient),
            # 菜单
            (r'/menu/', Menu),
            # 二进制菜单
            (r'/binmenu/', BinMenu),
            # 节点操作
            (r'/nodeoperate/', NodeOperate),
            # 获取所有用户
            (r'/user/', User_List),
            # 步骤表单
            (r'/stepform/', StepForm),
            # 充值
            (r'/order/', OrderInsert),
            # 订单接口
            (r'/orderlist/', OrderList),
            # 支付接口
            (r'/payali/', PayAli),
            # 回调地址
            (r'/pay_back/', Pay_Back),
            # 钱包充值接口
            (r'/setmoney/', SetMoney),
            # 退款接口
            (r'/refund/', RefundMoney),
            # 广告发布
            (r'/ad/', Ad),
            # 广告列表
            (r'/adall/', AdAll),
            # 广告成图
            (r'/adreporting/', Ads_Reporting),
            # 账户余额提现功能
            (r'/alipay_withdrawals/', AliPay_Withdrawals),

            ###########模拟题
            # 模拟登陆
            (r'/testlogin/', Test_login),
            (r'/testsend/', Test_Send),
            (r'/panduan/', Panduan),

            # 微信支付(现实环境)
            (r'/wx_pay/', WX_pay),

            # (r'/user/', UserHandler),
            # # //邮箱激活
            # (r'/emailactive/', EmailActive),
            # (r'/emailback/', EmailBack),
            # # 邮箱验证码
            # (r'/emailcode/', EmailCode),
            # # 短信验证
            # (r'/phonelogin/', PhoneLogin),
            # # 获取短信验证码
            # (r'/phonecode/', PhoneCode),
            # # 三方登陆
            # (r'/geturl/', GetUrl),
            # # 回调的路由
            # (r'/gitee_back/', Gitee_Back),
            # # 用户信息接口
            # (r'/userinfo/', UserInfo),
            # # 绘制图片
            # (r'/mycode/', MyCode),
            # # 图片验证码判断
            # (r'/imgcode/', ImgCode),
            # # 钉钉回调地址
            # (r'/dingding_back/', DingDing_Back),
            # # 微博回调
            # (r'/weibo_back/', WeiBo_Back),
            # # 图片上传路径
            # (r'/upload/', Upload),
            #
            # # 帖子
            # (r'/sub/', SubjectView),
            # # //获取用户信息
            # (r'/getuser/', GetUser),
            # # 获取关注人信息
            # (r'/ifconcern/', IfConcern),
            # # 帖子图片存mongo
            # (r'/img/', Img),
            # # 单帖子详情
            # (r'/onesub/', OneSub),
            # # 帖子评论
            # (r'/comment/', CommentView),
            #
            # # 考试题
            # (r'/products/', GetProducts),
            # (r'/orders/', Orders),
            # (r'/test_login/', Test_Login),
            # (r'/test_comment/', Comment),
            # (r'/klogin/', KLogin),
            # (r'/k_shop/', KShop),
            # (r'/k_cate/', KCate),

        ], static_path=static_path,
            debug=True
    )
    # 将异步数据库操作，注入tornado
    app.objects = peewee_async.Manager(database)

    # 声明服务器  用来执行tornado实例         (模拟https协议)ssl_options
    server = httpserver.HTTPServer(app, xheaders=True,
                                   ssl_options={
                                       # 证书文件
                                       'certfile': './localhost.crt',
                                       # 密钥文件
                                       'keyfile': './localhost.key'
                                   }
                                   )

    # 指定端口号
    # server.listen(8888)
    server.listen(443)

    # 执行事件循环 ioloop  事件驱动编程  io布鲁  
    tornado.ioloop.IOLoop.instance().start()


if __name__ == "__main__":
    print('服务已启动')
    main()
